﻿using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace Hr.Models
{
    public static class JsonHelper
    {
        public static string SerializeObject(object obj, string dateTimeFormatter = @"yyyy-MM-dd")
        {
            var isoDataFormatter = new IsoDateTimeConverter()
            {
                DateTimeFormat = dateTimeFormatter
            };
            return JsonConvert.SerializeObject(obj, isoDataFormatter);
        }

        public static string SerializeObject(object obj, string dateTimeFormatter, bool enumValue)
        {
            if (enumValue) return SerializeObject(obj, dateTimeFormatter);

            var setting = new JsonSerializerSettings()
            {
                Converters = new List<JsonConverter>
                {
                    new StringEnumConverter(),
                new IsoDateTimeConverter()
                {
                    DateTimeFormat = dateTimeFormatter
                }
                }
            };
            return JsonConvert.SerializeObject(obj, setting);
        }

        public static T DeserializeObject<T>(string jsonString) where T : class
        {
            return JsonConvert.DeserializeObject<T>(jsonString);
        }
    }
}